#region

using System;
using System.ComponentModel;
using System.Text;
using System.Timers;
using DataModel;
using HardwareDriver;
using HomeController.ControllerServiceReference;

#endregion

namespace HomeController
{
    public class ControllerViewModel : INotifyPropertyChanged
    {
        #region filds

        K8055Controller Controller = new K8055Controller();
        private bool _isDevice1Enabled;
        Timer timer = new Timer();
        private bool _isDevice2Enabled;

        private bool _isDevice3Enabled;

        private bool _isDevice4Enabled;

        private bool _isDevice5Enabled;

        private bool _isDevice6Enabled;

        private ControllerServiceReference.ControllerServiceClient _controllerServiceClient =
            new ControllerServiceClient();

        #endregion

        #region Properties

        private bool _isBusy;
        private bool fromServer = false;

        public bool IsDevice1Enabled
        {
            get { return _isDevice1Enabled; }
            set
            {
                _isDevice1Enabled = value;
                OnPropertyChanged("IsDevice1Enabled");
            }
        }

        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                OnPropertyChanged("IsBusy");
            }
        }

        public bool IsDevice2Enabled
        {
            get { return _isDevice2Enabled; }
            set
            {
                _isDevice2Enabled = value;
                OnPropertyChanged("IsDevice2Enabled");
            }
        }

        public bool IsDevice3Enabled
        {
            get { return _isDevice3Enabled; }
            set
            {
                _isDevice3Enabled = value;
                OnPropertyChanged("IsDevice3Enabled");
            }
        }

        public bool IsDevice4Enabled
        {
            get { return _isDevice4Enabled; }
            set
            {
                _isDevice4Enabled = value;
                OnPropertyChanged("IsDevice4Enabled");
            }
        }

        public bool IsDevice5Enabled
        {
            get { return _isDevice5Enabled; }
            set
            {
                _isDevice5Enabled = value;
                OnPropertyChanged("IsDevice5Enabled");
            }
        }

        public bool IsDevice6Enabled
        {
            get { return _isDevice6Enabled; }
            set
            {
                _isDevice6Enabled = value;
                OnPropertyChanged("IsDevice6Enabled");
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
                if (propertyName.StartsWith("IsDevice"))
                {
                    if (!fromServer)
                    {
                        this.SubmitToServer();
                    }
                }
            }
        }

        K8055Data _data = new K8055Data();

        private void SubmitToServer()
        {
            _data.OutputPin1 = IsDevice1Enabled;
            _data.OutputPin2 = IsDevice2Enabled;
            _data.OutputPin3 = IsDevice3Enabled;
            _data.OutputPin4 = IsDevice4Enabled;
            _data.OutputPin5 = IsDevice5Enabled;
            _data.OutputPin6 = IsDevice6Enabled;

            Controller.WriteData(_data);
        }

        public ControllerViewModel()
        {
            InitializationHardware();

            timer.Elapsed += UpdateMySelf;
            timer.Interval = 5000;
            // timer.Start();
        }

        internal void InitializationHardware()
        {
            Controller.Initialize();
        }

        private string Decrept(byte[] cypher)
        {
            // create a new instance of the algorithm to decrypt
            ElGamal x_decrypt_alg = new ElGamalManaged();
            // set the keys - note that we export with the
            // private parameters since we are decrypting data
            x_decrypt_alg.FromXmlString(ElgmalKeyDistro.GetElgamal().ToXmlString(true));
            // restore the plaintext
            byte[] x_candidate_plaintext = x_decrypt_alg.DecryptData(cypher);

            return
                Encoding.Default.GetString(x_candidate_plaintext);

            //   Console.WriteLine("OAEP KEY EXCHANGE: {0}", CompareArrays(x_plaintext, x_candidate_plaintext));
        }

        public void UpdateMySelf(object sender, ElapsedEventArgs e)
        {
            string myData = _controllerServiceClient.GetMyData();

            try
            {
                fromServer = true;
                int serverValue = 0;
                try
                {
                    serverValue = int.Parse(myData);
                }
                catch (Exception)
                {
                }
                _data.Output |= serverValue;

                IsDevice1Enabled = _data.OutputPin1;
                IsDevice2Enabled = _data.OutputPin2;
                IsDevice3Enabled = _data.OutputPin3;
                IsDevice4Enabled = _data.OutputPin4;
                IsDevice5Enabled = _data.OutputPin5;
                IsDevice6Enabled = _data.OutputPin6;

                fromServer = false;
                Controller.WriteData(_data);
            }
            catch (Exception)
            {
            }
        }

        public void CloseControler()
        {
            Controller.Close();

            timer.Stop();
            timer.Enabled = false;
        }
    }
}